package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * 时间传感器心跳包解析
 */
public class TimeHeartBean {

    public int  index = -1; //传感器序号
    public float timeVel = -1; //数值
    public int signal ; //信号值
    public int power; //电量值
    public long time ; //数据包时间
    public ComBean comBean;

    public TimeHeartBean() {

    }

    public TimeHeartBean(ComBean comBean ) {
        caculate(comBean.recData);
        time = comBean.recTime;
        this.comBean = comBean;
    }

    public TimeHeartBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public TimeHeartBean caculate(ComBean comBean){
        time = comBean.recTime;
        this.comBean = comBean;
        return caculate(comBean.recData);
    }

    public TimeHeartBean caculate(byte[] buffer) {
        signal = MyFunc.getSignal(buffer[16]); //测试信号
        power = MyFunc.getPower(buffer[14], buffer[15]);//电量
        index = buffer[10];
        return this;
    }

    public void  test(int index){
        time = System.currentTimeMillis();
        power = 300 + myRandom.nextInt(120);
        signal = 200;
        timeVel = myRandom.nextInt(10) + myRandom.nextFloat();
        this.index =index;
    }
}
